/*****************************************************************************
 *
 * Øving 3 uCSysDes, driverkoden
 *
 *****************************************************************************/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/ioport.h>
#include <asm/io.h>
#include <asm/uaccess.h>

#include "ap7000.h"

/* prototyper */

static int __init driver_init(void);
static void __exit driver_exit(void);
static int driver_open (struct inode *inode, struct file *filp);
static int driver_release (struct inode *inode, struct file *filp);
static int driver_read (struct file *filp, char __user *buff,
                     size_t count, loff_t *offp);
static int driver_write (struct file *filp, const char __user *buff,
                      size_t count, loff_t *offp);

/* fops-struct */

static struct file_operations driver_fops = {
  .owner = THIS_MODULE,
  .read = driver_read,
  .write = driver_write,
  .open = driver_open,
  .release = driver_release
};

/* char device struct */
static struct cdev *driver_dev;
/* device dates */
/* global variables for major and minor device number
   when major no. is 0 dynamic allocation is used */
static unsigned int dev_minor = 0;
static unsigned int dev_major;// = 60;//TESTING!!
// number of requested devive numbers
static const unsigned int dev_no = 1;
static const char* dev_name = "led_button_driver";
//needed PIO-registers including PUER
static const int pio_region_length = 0x78;
/*variables for hardware access */
// Port B
// LEDs: pins 0-7, buttons: pins 8-10,13-16,30
static volatile avr32_pio_t *piob = &AVR32_PIOB;
static const int enabled_buttons = 0x4001e700;

/*****************************************************************************/
/* init-funksjon (kalles når modul lastes) */

static int __init driver_init (void) {
  int result = 0;
  
  // allocate cahr device memory space
  driver_dev = cdev_alloc();
  /* allokere device-nummer */
  result = alloc_chrdev_region(&driver_dev->dev, dev_minor, dev_no, dev_name);
  
  if(result < 0) {
    printk(KERN_WARNING "no major number assigned");
    return result;
  } else {
    printk(KERN_WARNING "Major: %d, Minor: %d",MAJOR(driver_dev->dev),MINOR(driver_dev->dev));
  }
  
  /* be om tilgang til I/O-porter */
   //region for buttons and LEDs
  if(request_region(AVR32_PIOB_ADDRESS, pio_region_length, dev_name) == NULL){
    printk(KERN_WARNING "request region failed");
  }  

  /* initialisere PIO-maskinvaren (som i øving 2) */
  piob->pdr = ~0; //disable all pins before enable the needed ones
  piob->per = 0xff; //enable LED pins
  piob->PER.p8 = 1;
  piob->PER.p9 = 1;
  piob->PER.p10 = 1;
  piob->PER.p13 = 1;
  piob->PER.p14 = 1;
  piob->PER.p15 = 1;
  piob->PER.p16 = 1;
  piob->PER.p30 = 1;
  piob->oer = 0xff; // enable LEDS as outputs
  piob->puer = 0;
  piob->PUER.p8 = 1;
  piob->PUER.p9 = 1;
  piob->PUER.p10 = 1;
  piob->PUER.p13 = 1;
  piob->PUER.p14 = 1;
  piob->PUER.p15 = 1;
  piob->PUER.p16 = 1;
  piob->PUER.p30 = 1;
  piob->codr = ~0; //erase old values
  piob->sodr = 0x18; //turn on leds with init value

  /* registrere device i systemet (må gjøres når alt annet er initialisert) */
  driver_dev->ops = &driver_fops;
  driver_dev->owner = THIS_MODULE;
  result = cdev_add(driver_dev,driver_dev->dev, 1);
  if(result < 0){
    printk(KERN_WARNING "cdev_add() failed");
    return result;
  }
  printk(KERN_WARNING "Driver loaded!");
  return result;
}

/*****************************************************************************/
/* exit-funksjon (kalles når modul fjernes fra systemet) */

static void __exit driver_exit (void) {
  
  //release hardware
  //release_region(AVR32_PIOB_ADDRESS, pio_region_length);
  cdev_del(driver_dev);
  kfree(driver_dev);
  printk(KERN_WARNING "Driver released!");
}

/*****************************************************************************/
/* fops-funksjoner */

static int driver_open (struct inode *inode, struct file *filp) {
  return 0;
}

/*---------------------------------------------------------------------------*/

static int driver_release (struct inode *inode, struct file *filp) {
  return 0;
}

/*---------------------------------------------------------------------------*/

static ssize_t driver_read (struct file *filp, char __user *buff,
              size_t count, loff_t *offp) {
  int button_buf;
  int temp = 0;

  button_buf = (~piob->pdsr)&(enabled_buttons);

  /* considered pins:
  p8,p9,p10,p13,p14,p15,p16,p30
  0100 0000 0000 0001 1110 0111 0000 0000
  0x4001e700
  pins:
  0 -> 0x100
  1 -> 0x200
  2 -> 0x400
  3 -> 0x2000
  4 -> 0x4000
  5 -> 0x8000
  6 -> 0x10000
  7 -> 0x40000000 */
  if(button_buf & 0x100) {temp = temp | 0x1;}
  if(button_buf & 0x200) {temp = temp | 0x2;}
  if(button_buf & 0x400) {temp = temp | 0x4;}
  if(button_buf & 0x2000) {temp = temp | 0x8;}
  if(button_buf & 0x4000) {temp = temp | 0x10;}
  if(button_buf & 0x8000) {temp = temp | 0x20;}
  if(button_buf & 0x10000) {temp = temp | 0x40;}
  if(button_buf & 0x40000000) {temp = temp | 0x80;}
  
  copy_to_user(buff,&temp,sizeof(temp));
  
  return 0;
}

/*---------------------------------------------------------------------------*/

static ssize_t driver_write (struct file *filp, const char __user *buff,
               size_t count, loff_t *offp) {
  int led_buf;
  int result = 0;

  if(copy_from_user(&led_buf,buff,sizeof(buff))) {
    result = -EFAULT;
    printk(KERN_WARNING "error in copy_from_user");
    return -1;
  }  

  piob->codr = 0xff; //erase old values
  piob->sodr = led_buf; //turn on leds
  
  return count;
}

/*****************************************************************************/
/* modul-beskrivelsesmakroer */

module_init (driver_init);  /* angir hva som er init-funksjon */
module_exit (driver_exit);  /* angir hva som er exit-funksjon */

MODULE_LICENSE ("GPL");     /* programlisens for modulen */
MODULE_DESCRIPTION ("Device driver for buttons and LEDs on STK1000 from Atmel");    /* tekstlig beskrivelse */
MODULE_VERSION ("");        /* versjonsnummer */
MODULE_AUTHOR ("R. Braunschweig, K. Guldbrandsen");   /* forfatter(e) */

